package eco2automotive.tasks;
import java.util.*;
import eco2automotive.port.ConnectionControl;
import eco2automotive.gui.InforAlert;
import eco2automotive.elmFilter.Filter;
import eco2automotive.gui.Layout_DTCs;
import eco2automotive.elmFilter.SpecialFormula;
import eco2automotive.lang.EN;
import eco2automotive.data.HashErrorCode;
import eco2automotive.data.ErrorCode;
/**   
 * Query, number of trouble codes or errors
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  *
  */ 
public class Task_DTCs{
    ConnectionControl connControl;        
    Layout_DTCs LAYOUT_DTCs;    
    HashErrorCode HERRORCODES;
    /**
     * Take the control and update the laytout
     * @param connControl
     * @param LAYOUT_DTCs
     * @param HERRORCODES
     */
    public Task_DTCs(ConnectionControl connControl, Layout_DTCs LAYOUT_DTCs,HashErrorCode HERRORCODES)
    {       
        this.connControl=connControl;                 
        this.LAYOUT_DTCs=LAYOUT_DTCs;
        this.HERRORCODES=HERRORCODES;
        
    }
    
    /**
     * start
     */
    public void run()
   {        
      
        new InforAlert("WAIT!",EN.QUICK_ACTIONS_QUERY_DTCS_KEYON,2).showAlert();                                  
        LAYOUT_DTCs.updatePrgogress("Cheking......");
        connControl.TXData("0101");            //Check MIL is Turn On?        
        connControl.TXData("03");              //Number of errors
        try{Thread.sleep(3000);}catch(Exception e){}  //Wait 3"                                          
        LAYOUT_DTCs.updatePrgogress("Finish!");            
if (connControl.DATA_AVAILABLE()==false)
      {                                         
      new InforAlert("#ERROR!",EN.QUICK_ACTIONS_QUERY_NO_RESPONSE,1).showAlert();
        }
else{ //DATA FOUND!                
         boolean found=false;
         int errors;            
         for(int i=0;i<=connControl.getFata().getLength();i++){  //FOR
            String data=connControl.getFata().extract().toString();  
            System.out.println("PILA GETDATA "+i+" datos"+data);
            String str_p0101=new Filter().getDigest(data,"4101[0-9-A-Z]{8}");
            String str_p03=new Filter().getDigest(data,"43[0-9-A-Z]{12}"); 
            /* 
             * Try this, based on a real monitoring
            str_p0101="410181076504";            
            str_p03="43017300000000";
            */
            errors=0;            
            if(str_p0101.length()==12){ //If contains 12 digits hex. (eg. 410181076504 lenght=12 and HEAD=4101)
                        str_p0101=str_p0101.substring(4,str_p0101.length());    //Substract 4101                                       
                        errors=new SpecialFormula().pid_0101(str_p0101);        //Get number of errors
                       new InforAlert("#DTCs",EN.QUICK_ACTIONS_QUERY_DTCS_NUMOF_ERRORS+errors,2).showAlert();                             
            }
            
           if(str_p03.length()==14) //14 digits hex. (eg. 43017300000000 lenght=14 and HEAD=43)
              str_p03=str_p03.substring(2,str_p03.length()); //Substract 43                                                        
             if(errors>0){                       
                       found=true;            
                       ArrayList array=new SpecialFormula().pid_03(str_p03);  //array wiht the trouble codes found
                       for ( Iterator iterador = array.listIterator(); iterador.hasNext(); ) {
                            String code=(String) iterador.next();
                            ErrorCode error= new ErrorCode();                    
                            error=HERRORCODES.getErrorCode(code);
                                if(error==null)//Not Found.
                                    {
                                    this.LAYOUT_DTCs.showInfoError("\n Error Found: "+code+"\tUnknow");
                                    }
                                else{
                                    this.LAYOUT_DTCs.showInfoError("\n Error Found: "+code+" "+error.getDescription());
                                    }
                            }     
                    }
             else if(errors==0)            
                    {

                      this.LAYOUT_DTCs.showInfoError("\t\n No Data Trouble Codes Found. "+data);
                     }
       } //END FOR ---------------------------------------------------
        }//END  ELSE   

                
   }
    
}
